package com.yunkeji.api.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunkeji.api.user.mapper.ApiUserBlackListMapper;
import com.yunkeji.api.user.service.AppUserBlackService;
import com.yunkeji.api.user.vo.BlackListVo;
import com.yunkeji.common.core.domain.PageQuery;
import com.yunkeji.common.core.page.TableDataInfo;
import com.yunkeji.common.exception.AppServiceException;
import com.yunkeji.common.helper.LoginHelper;
import com.yunkeji.dao.domain.BaseId;
import com.yunkeji.dao.domain.UserBlackList;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@AllArgsConstructor
public class AppUserBlackServiceImpl implements AppUserBlackService {

    private final ApiUserBlackListMapper apiUserBlackListMapper;

    /**
     * 黑名单列表
     *
     * @author 老张
     */
    @Override
    public TableDataInfo<BlackListVo> blackList(PageQuery pageQuery) {
        Page<BlackListVo> data =
                apiUserBlackListMapper.blackList(LoginHelper.getUserId(), pageQuery.build());
        return TableDataInfo.build(data);
    }

    /**
     * 移除黑名单
     *
     * @author 老张
     */
    @Override
    public void removeBlack(BaseId baseId) {
        apiUserBlackListMapper.delete(
                Wrappers.lambdaQuery(UserBlackList.class)
                        .eq(UserBlackList::getToUserId, baseId.getId())
                        .eq(UserBlackList::getFromUserId, LoginHelper.getUserId()));
    }

    /**
     * 加入黑名单
     *
     * @author 老张
     */
    @Override
    public void addBlack(BaseId baseId) {
        try {
            UserBlackList.builder()
                    .fromUserId(LoginHelper.getUserId())
                    .toUserId(baseId.getId())
                    .build()
                    .insert();
        } catch (Exception e) {
            throw AppServiceException.paramException("已经在黑名单了");
        }
    }
}
